Fix abort in top_k for k=0 - #3022
Merged
Merged
Conversation
top_k(x, 0) is a valid request whose result is empty, but py_topk still dispatched to the kernel, which builds an nd_range with a zero global size (nelems = iter_nelems * k == 0) and a non-zero local size. That is a silent no-op on runtimes built with NDEBUG, but aborts on an assertions-enabled SYCL runtime (adjustNDRangePerKernel asserts NDR.LocalSize[0] == 0 when GlobalSize is 0). Extend the early-return guard to cover k == 0. The vals/inds outputs are already allocated with a zero-length result axis, so there is nothing to compute or fill.
Contributor
|
View rendered docs @ https://intelpython.github.io/dpnp/index.html |
Collaborator
antonwolfy
marked this pull request as ready for review
August 12, 2026 15:37
antonwolfy
requested review from
ndgrigorian and
vlad-perevezentsev
as code owners
August 12, 2026 15:37
Contributor
|
Array API standard conformance tests for dpnp=0.21.0dev3=py314h509198e_41 ran successfully. |
ndgrigorian
approved these changes
Aug 13, 2026
github-actions Bot
added a commit
that referenced
this pull request
Aug 13, 2026
`dpnp.tensor.top_k(x, 0)` might aborted the process on an assertions-enabled SYCL runtime: ```python import dpnp.tensor as dpt dpt.top_k(dpt.arange(12).reshape(3, 4), 0) # k = 0 ``` `k == 0` is a valid request whose result is empty, and it passes validation (`k < 0` is rejected, `k > size` is rejected, but `k == 0` is allowed). `py_topk` then dispatched to the kernel, whose output-writing step (`write_out_impl`) builds an `nd_range` from `nelems = iter_nelems * k`. A zero-sized `nd_range` is a silent no-op on runtimes built with `NDEBUG`, but aborts on an assertions-enabled SYCL runtime. The existing guard covered `iter_nelems == 0` and `axis_nelems == 0`, but not `k == 0`. This PR proposes to extend the early-return guard in `py_topk` to also cover `k == 0`. 9b62c27
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
dpnp.tensor.top_k(x, 0)might aborted the process on an assertions-enabled SYCL runtime:k == 0is a valid request whose result is empty, and it passes validation (k < 0is rejected,k > sizeis rejected, butk == 0is allowed).py_topkthen dispatched to the kernel, whose output-writing step (write_out_impl) builds annd_rangefromnelems = iter_nelems * k. A zero-sizednd_rangeis a silent no-op on runtimes built withNDEBUG, but aborts on an assertions-enabled SYCL runtime.The existing guard covered
iter_nelems == 0andaxis_nelems == 0, but notk == 0.This PR proposes to extend the early-return guard in
py_topkto also coverk == 0.